function newCollisionMap( imageFileName, offsetX, offsetY )

	local offsetX = offsetX
	local offsetY = offsetY

	local imageData = love.image.newImageData( imageFileName )
	local collisionMap = {}
	
	for x = 1, 90 do
		collisionMap[x] = {}
		for y = 1, 60 do
			collisionMap[x][y] = 0
		end
	end
	
	for y = offsetY, offsetY + 59 do
		for x = offsetX, offsetX + 89 do
		
			local r, g, b, a = imageData:getPixel( x, y )
			if a == 0 then
				collisionMap[x - offsetX + 1][y - offsetY + 1] = 0
			else
				collisionMap[x - offsetX + 1][y - offsetY + 1] = 1
			end
			
		end
	end
	return collisionMap
end

function perPixelCollision( obj1, tile, tiles )

    -- First, we do a simple bounding box collision check. This will let
    -- us know if the two objs overlap in any way.
    if not ( (obj1.getX() + obj1.getImage():getWidth() > tile.screenx) and
             (obj1.getX() < tile.screenx + tile.getImage():getWidth()) and
             (obj1.getY() + obj1.getImage():getHeight() > tile.screeny) and
             (obj1.getY() < tile.screeny + tile.getImage():getHeight()) ) then
        return false
    end

    -- If we made it this far, our two objs definitely touch or overlap,
    -- but that doesn't mean that that we have an actual collision between
    -- two non-transparent pixels.

    -- By default, obj1 scans tile for a pixel collision per line, so
    -- if obj1 is taller, swap the objs around so the shorter one is
    -- scanning the taller one. This will result in less work in cases where 
    -- they initially overlap but ultimately do not collide at the pixel level.
    if obj1.getImage():getHeight() > tile.getImage():getHeight() then
       objTemp = obj1
       obj1 = tile
       obj1 = objTemp
    end

    -- Loop through each row of obj1's collision map and check it against
    -- tile's corresponding collision map row.
    for indexY = 1, obj1.getImage():getHeight() do

        local screenY = math.floor( (obj1.getY() + indexY) - 1  )

        if screenY > tile.screeny and screenY <= tile.screeny + tile.getImage():getHeight() then

            -- Some, or all, of the current row (Y) of obj1's collision map overlaps
            -- tile's collision map. Calculate the start and end indices (X) for each
            -- row, so we can test this area of overlap for a collision of 
            -- non-transparent pixels.

            local y1 = math.floor( indexY )
            local y2 = 1

            if screenY > tile.screeny then
                y2 = math.floor( screenY - tile.screeny )
            elseif screenY < tile.screeny then
                y2 = math.floor( tile.screeny - screenY )
            end

            local obj1Index1 = 1
            local obj1Index2 = obj1.getImage():getWidth()
            local tileIndex1 = 1
            local tileIndex2 = tile.getImage():getWidth()

            if obj1.getX() < tile.screenx then

               obj1Index1 = math.floor( tile.screenx - obj1.getX() ) + 1
               obj1Index2 = obj1.getImage():getWidth()

               tileIndex1 = 1
               tileIndex2 = math.floor( (obj1.getX() + obj1.getImage():getWidth()) - tile.screenx ) + 1

               -- If the objs being tested are of different sizes it's possible
               -- for this index to get too big - so clamp it.
               if tileIndex2 > tile.getImage():getWidth() then
                  tileIndex2 = tile.getImage():getWidth()
               end

            elseif obj1.getX() > tile.screenx then

               obj1Index1 = 1
               obj1Index2 = math.floor( (tile.screenx + tile.getImage():getWidth()) - obj1.getX() ) + 1

               -- If the objs being tested are of different sizes it's possible
               -- for this index to get too big - so clamp it.
               if obj1Index2 > obj1.getImage():getWidth() then
                  obj1Index2 = obj1.getImage():getWidth()
               end

               tileIndex1 = math.floor( obj1.getX() - tile.screenx ) + 1
               tileIndex2 = tile.getImage():getWidth()

            else -- obj1.getX() == tile.screenx

               -- If the two objs have the same x position - the width of 
               -- overlap is simply the shortest width.
               shortest = obj1.getImage():getWidth()

               if tile.getImage():getWidth() < shortest then
                  shortest = tile.getImage():getWidth()
               end

               obj1Index1 = 1
               obj1Index2 = shortest

               tileIndex1 = 1
               tileIndex2 = shortest

            end

            local index1 = obj1Index1
            local index2 = tileIndex1

            while index1 < obj1Index2 and index2 < tileIndex2 do

                if obj1.collisionMap[y1][index1] == 1 and tile.collisionMap[y2][index2] == 1 then
                    return true -- We have a collision of two non-transparent pixels!
                end

                index1 = index1 + 1
                index2 = index2 + 1

            end

        end

    end

    return false -- We do NOT have a collision of two non-transparent pixels.

end

--[[
function love.load()

    sprites = {}

    local terrainSprite =
    {
        name = "Terrain",
        image = love.graphics.newImage( "terrain.png" ),
        collisionMap = newCollisionMap( "terrain.png" ),
        x = 0,
        y = 440
    }

    table.insert( sprites, terrainSprite )

    local bushSprite =
    {
        name = "Bush",
        image = love.graphics.newImage( "bush.png" ),
        collisionMap = newCollisionMap( "bush.png" ),
        x = 500,
        y = 425
    }

    table.insert( sprites, bushSprite )

    local treeSprite =
    {
        name = "Tree",
        image = love.graphics.newImage( "tree.png" ),
        collisionMap = newCollisionMap( "tree.png" ),
        x = 50,
        y = 185
    }

    table.insert( sprites, treeSprite )

    local cloudSprite =
    {
        name = "Cloud",
        image = love.graphics.newImage( "cloud_plain.png" ),
        collisionMap = newCollisionMap( "cloud_plain.png" ),
        x = 300,
        y = 25
    }

    table.insert( sprites, cloudSprite )

    local loveLogoSprite =
    {
        name = "LOVE Logo",
        image = love.graphics.newImage( "love_logo.png" ),
        collisionMap = newCollisionMap( "love_logo.png" ),
        x = 350,
        y = 275
    }

    table.insert( sprites, loveLogoSprite )

    font = love.graphics.newFont( 12 )
    love.graphics.setFont( font )
    message = "No pixels colliding!"
    enableCollisionResponse = false

    -- Light blue color for the sky (HTML).
    love.graphics.setBackgroundColor( 0x84, 0xCA, 0xFF )

end

function love.keypressed( key )

    -- Space bar
    if key == " " then
        enableCollisionResponse = not enableCollisionResponse
    end

end

function love.update( dt )
    
    -- Cache the old x,y just in case we need to prevent the LOVE logo
    -- from entering another sprite.
    local x = sprites[5].getX()
    local y = sprites[5].getY()

    if love.keyboard.isDown( "left" ) then
       sprites[5].getX() = sprites[5].getX() - 200 * dt
    end

    if love.keyboard.isDown( "right" ) then
       sprites[5].getX() = sprites[5].getX() + 200 * dt
    end

    if love.keyboard.isDown( "up" ) then
       sprites[5].getY() = sprites[5].getY() - 200 * dt
    end

    if love.keyboard.isDown( "down" ) then
       sprites[5].getY() = sprites[5].getY() + 200 * dt
    end

    local colliding = false
    local name = ""

    for i,v in ipairs( sprites ) do

        -- If the sprite is not number 5, which is our LOVE logo, test the 
        -- sprite for per pixel collision against our ball sprite.
        if i ~= 5 then
            if perPixelCollision( sprites[5], v ) then
                colliding = true

                if name == "" then
                    name = '"' .. v.name .. '"'
                else
                    name = name .. " and " .. '"' .. v.name .. '"'
                end

            end
        end
    end

    if colliding then
        message = "LOVE logo pixels colliding with " .. name .. "!"

        -- If collision response is enabled and we have collided with another
        -- sprite - simply disallow the move by restoring its old x, y position.
        if enableCollisionResponse == true then
            sprites[5].getX() = x
            sprites[5].getY() = y
        end
    else
       message = "No pixels colliding!"
    end

end

function love.draw()

    for i,v in ipairs( sprites ) do
        love.graphics.draw( v.getImage(), v.getX(), v.getY() )
    end

    love.graphics.print( "Use the arrow keys to move the LOVE logo.", 10, 20 )

    if enableCollisionResponse == true then
       love.graphics.print( "Use the Space bar to toggle a collision response. (ON)", 10, 40 )
    else
        love.graphics.print( "Use the Space bar to toggle a collision response. (OFF)", 10, 40 )
    end

    love.graphics.print( message, 10, 60 )

end
]]

